package com.vinaprime.primeschedule.databaselayer;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.vinaprime.primeschedule.entities.VPConfiguration;

public class VPConfiguration_DL {

	private static final String DB_TABLE_NAME = "VP_CONFIGURATION";
	private static final String DB_TABLE_COL_ROWID = "_id";
	private static final String DB_TABLE_COL_ID = "VPC_ID";
	private static final String DB_TABLE_COL_EMAIL = "VPC_EMAIL";
	private static final String DB_TABLE_COL_LANGUAGE_ID = "VPC_LANGUAGE_ID";
	private static final String DB_TABLE_COL_SNOOZE_SOUND_ID = "VPC_SNOOZE_SOUND_ID";
	private static final String DB_TABLE_COL_FORCE_STOP_FLAG = "VPC_FORCE_STOP_FLAG";
	
	private DatabaseController_DL myDatabaseControllerDL;
	
	public VPConfiguration_DL(Context context, String path)
	{
		myDatabaseControllerDL = new DatabaseController_DL(context, path);
	}

	public static String getDbTableName() {
		return DB_TABLE_NAME;
	}
	
	public static String getDbTableColRowid() {
		return DB_TABLE_COL_ROWID;
	}

	public static String getDbTableColId() {
		return DB_TABLE_COL_ID;
	}

	public static String getDbTableColEmail() {
		return DB_TABLE_COL_EMAIL;
	}

	public static String getDbTableColLanguageId() {
		return DB_TABLE_COL_LANGUAGE_ID;
	}

	public static String getDbTableColSnoozeSoundId() {
		return DB_TABLE_COL_SNOOZE_SOUND_ID;
	}

	public static String getDbTableColForceStopFlag() {
		return DB_TABLE_COL_FORCE_STOP_FLAG;
	}
	
	public Cursor getConfiguration()
	{
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		String selectQuery = "SELECT  CONF." + VPConfiguration_DL.DB_TABLE_COL_ID + " AS " + VPConfiguration_DL.DB_TABLE_COL_ROWID + ", CONF." +
											VPConfiguration_DL.DB_TABLE_COL_ID + ", CONF." +
											VPConfiguration_DL.DB_TABLE_COL_EMAIL + ", CONF." +
											VPConfiguration_DL.DB_TABLE_COL_LANGUAGE_ID + ", LANG." +
											VPLanguage_DL.getDbTableColCode() + ", LANG." +
											VPLanguage_DL.getDbTableColName() + ", CONF." +
											VPConfiguration_DL.DB_TABLE_COL_SNOOZE_SOUND_ID + ", SOUN." +
											VPSnoozeSound_DL.getDbVpssFilePath() + ", SOUN." +
											VPSnoozeSound_DL.getDbvpssSound() +", CONF." +
											VPConfiguration_DL.DB_TABLE_COL_FORCE_STOP_FLAG +
								" FROM " + VPConfiguration_DL.DB_TABLE_NAME + " CONF " +
											" LEFT OUTER JOIN " + VPLanguage_DL.getDbTableName() + " LANG " +
											" ON CONF." + VPConfiguration_DL.DB_TABLE_COL_LANGUAGE_ID + " = LANG." + VPLanguage_DL.getDbTableColId() +
											" LEFT OUTER JOIN " + VPSnoozeSound_DL.getDbTableName() + " SOUN " +
											" ON CONF." + VPConfiguration_DL.DB_TABLE_COL_SNOOZE_SOUND_ID + " = SOUN." + VPSnoozeSound_DL.getDbTableVpssId() +
								" WHERE 1 = 1";
		
		Cursor cur = db.rawQuery(selectQuery, null);
		
		return cur;
	}
	
	public void updateConfiguration(VPConfiguration config){
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
        
        // updating row
        ContentValues values = new ContentValues();
        values.put(VPConfiguration_DL.DB_TABLE_COL_ID, config.getVpcId());
        values.put(VPConfiguration_DL.DB_TABLE_COL_EMAIL, config.getVpcEmail());
        values.put(VPConfiguration_DL.DB_TABLE_COL_LANGUAGE_ID, config.getVpcLanguage().getVplId());
        values.put(VPConfiguration_DL.DB_TABLE_COL_SNOOZE_SOUND_ID, config.getVpcSnoozeSound().getVpssId());
        values.put(VPConfiguration_DL.DB_TABLE_COL_FORCE_STOP_FLAG, config.getVpcForceStopFlag());
        
        db.update(VPConfiguration_DL.DB_TABLE_NAME, values, VPConfiguration_DL.DB_TABLE_COL_ID + " = ?", new String[] {String.valueOf(config.getVpcId())});
        db.close(); // Closing database connection
		
	}
}
